package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.pojo.bo.monitor.LoginLogBO;
import com.lwc.cfdns.pojo.entity.SystemLogininfor;
import com.lwc.cfdns.pojo.vo.monitor.LoginLogVO;
import com.lwc.cfdns.service.SystemLogininforService;
import com.lwc.cfdns.mapper.SystemLogininforMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.ServletUtils;
import com.lwc.cfdns.utils.ip.AddressUtils;
import com.lwc.cfdns.utils.ip.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_logininfor(系统访问记录)】的数据库操作Service实现
 * @createDate 2023-07-24 16:16:34
 */
@Service
public class SystemLogininforServiceImpl extends ServiceImpl<SystemLogininforMapper, SystemLogininfor> implements SystemLogininforService {
    private static final Logger logger = LoggerFactory.getLogger(SystemLogininforServiceImpl.class);
    @Autowired
    private SystemAdminServiceImpl systemAdminService;

    /**
     * 异步执行
     *
     * @param userName
     * @param status
     * @param message
     * @param ipAddr
     * @param userAgent
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/25 14:29:34
     */
    @Async("threadPoolTaskExecutor")// 提交到线程池中去处理
    public void syncCreate(String userName, String status, String message, String ipAddr, UserAgent userAgent) {
        logger.info("登录log:{},{},{},{},{}。", userName, status, message, ipAddr, Thread.currentThread().getName());
        SystemLogininfor systemLogininfor = new SystemLogininfor();
        systemLogininfor.setMsg(message);
        systemLogininfor.setUserName(userName);
        systemLogininfor.setLoginTime(new Timestamp(System.currentTimeMillis()));
        systemLogininfor.setOs(userAgent.getOperatingSystem().getName());
        systemLogininfor.setBrowser(userAgent.getBrowser().getName());
        systemLogininfor.setStatus(status);
        systemLogininfor.setIpaddr(ipAddr);
        systemLogininfor.setLoginLocation((String) AddressUtils.getRealAddressByIP(ipAddr));
        this.baseMapper.insert(systemLogininfor);
    }

    /**
     * 创建
     *
     * @param
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/24 16:26:55
     */
    @Override
    public void create(String userName, String status, String message) {
        String ipAddr = IpUtils.getIpAddr();
        UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        syncCreate(userName, status, message, ipAddr, userAgent);
    }

    /**
     * 列表
     *
     * @param loginLogBO
     * @return IPage<SystemLogininfor>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/25 09:56:18
     */
    @Override
    public IPage<SystemLogininfor> lists(LoginLogBO loginLogBO) {
        Page<SystemLogininfor> page = this.baseMapper.selectPage(loginLogBO.toPage(), loginLogBO.toQueryWrapper());
        // 按OV返回
        page.convert(s -> {
            LoginLogVO loginLogVO = new LoginLogVO();
            BeanUtils.copyProperties(s, loginLogVO);
            return loginLogVO;
        });
        return page;
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/25 11:02:40
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemLogininfor>().in(SystemLogininfor::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 删除所有
     *
     * @param
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/25 11:02:51
     */
    @Override
    public Map<String, Object> clean() {
        int i = this.baseMapper.delete(new LambdaQueryWrapper<SystemLogininfor>().eq(SystemLogininfor::getDeleted, 0));
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 解锁
     *
     * @param userName
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/7/25 11:03:00
     */
    @Override
    public Map<String, Object> unlock(String userName) {
        // todo 密码错误几次锁定账号
        Map<String, Object> map = new HashMap<>();
        map.put("result", systemAdminService.clearLoginUnlock(userName));
        return map;
    }
}




